package UsersPackage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static java.lang.System.out;

@WebServlet("/update.do")
public class UpdateUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        // 获取session中的属性
        String username = (String) session.getAttribute("username");
        try {

            String user_id = request.getParameter("user_id");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String birth = request.getParameter("birth");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");

            UserDao userService = new UserDao();
            if(userService.updateUser(username, user_id, password, email, birth, phone, address) > 0) {
                response.getWriter().print("修改成功,在3秒后跳转回登录页面!");
                response.setHeader("Refresh", "3;url=menu/customer_home.jsp"); // 在3秒后跳转回登录页面
            }else {
                response.getWriter().print("修改失败,在3秒后跳转回登录页面");
                response.setHeader("Refresh", "3;url=menu/customer_home.jsp"); // 在3秒后跳转回登录页面
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
